import { router } from '../router/index';
import Cookies from "js-cookie";
import { getStore, setStore, clearStore } from "./storage";
let lastTime = new Date().getTime()
let currentTime = new Date().getTime()
let timeOut = 15 * 60 * 1000 //设置超时时间: 30分钟
// let timeOut = 15 * 1000 //设置超时时间: 30分钟

window.onload = function () {
    window.document.onmousedown = function () {
        setStore("lastTime", new Date().getTime())
    }
};

function checkTimeout() {
    currentTime = new Date().getTime()     //更新当前时间
    lastTime = getStore("lastTime");
    console.log(currentTime - lastTime);
    if (currentTime - lastTime > timeOut) { //判断是否超时
        console.log(router.history.current.name);
        if (router.history.current.name == 'data' || router.history.current.name == 'login' || router.history.current.name == 'home_index') return
        // 清除storage的数据(登陆信息和token)
        clearStore();
        Cookies.remove("accessToken");
        Cookies.remove('userInfo');

    }
}

export default function () {
    /* 定时器 间隔30秒检测是否长时间未操作页面 */
    window.setInterval(checkTimeout, 30000);
}